package org.adw.launcher2.ld.secret.ui;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.db.MyPhotoCache;
import org.adw.launcher2.ld.secret.dom.MyPhoto;
import org.adw.launcher2.ld.secret.ui.adapter.MyPhotoAdapter;
import org.adw.launcher2.ld.secret.ui.utils.Global;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.adw.launcher2.R;

public class MyNonHiddenPhotoActivity extends Activity {
	public final String TAG = getClass().getSimpleName();
	public MyPhotoAdapter photoAdapter;
	private EditText filterText = null;
	boolean isFilter = false;
	public static List<MyPhoto> listAfterHide;
	public ProgressDialog dialog;
	public ListView listPhoto;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_photo);
		
		listPhoto = (ListView) findViewById(R.id.listMyPhoto);
		Global.cr = getContentResolver();
		// Init db for test something related to database
		filterText = (EditText) findViewById(R.id.txtSearchPhoto);
		filterText.addTextChangedListener(watcher);
		loadPhotoThread();
	}
	private TextWatcher watcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {	
		}
		
		@Override
		public void afterTextChanged(Editable arg0) {
			List<MyPhoto> listFilter = new LinkedList<MyPhoto>();
			for(int i = 0; i<listAfterHide.size();i++){
				String absolutePath = listAfterHide.get(i).getAbsolutePathNoExtend() + listAfterHide.get(i).getFileExtend();
				String keyWord = arg0.toString().toLowerCase();
				
				String[] temp = absolutePath.split("/");
				String fileName = temp[temp.length-1];
				
				if(fileName.toLowerCase().contains(keyWord)){
					listFilter.add(listAfterHide.get(i));
				}
			}
			bindView(listFilter);
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
		filterText.removeTextChangedListener(watcher);
	}

	protected List<MyPhoto> loadPhotoThread() {
		

		final List<MyPhoto> list = new LinkedList<MyPhoto>();
		AsyncTask<Uri, Void, List<MyPhoto>> task = new AsyncTask<Uri, Void, List<MyPhoto>>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyNonHiddenPhotoActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected List<MyPhoto> doInBackground(Uri... uris) {
				MyPhotoCache cache = MyPhotoCache.getInstance(Global.dbHelper);
				return cache.getPhotosNoImageFromMemory(Global.cr);
			}

			@Override
			protected void onPostExecute(List<MyPhoto> result) {
				
				listAfterHide = result;
				Log.d("sizeOfList", result.size()+"");
				bindView(result);
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
				
			}
		};
		task.execute();
		Toast.makeText(this, R.string.loading_image, Toast.LENGTH_LONG).show();
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.my_contact_menu, menu);// xai chung menu cua contact
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.hide_contact:

			hideAllSelectedPhotos();
			break;
		case R.id.refresh_contact:
			bindView(listAfterHide);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void hideAllSelectedPhotos() {
		AsyncTask<Uri, Void, Void> task = new AsyncTask<Uri, Void, Void>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyNonHiddenPhotoActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected Void doInBackground(Uri... params) {
				for (int i = 0; i < MyPhotoAdapter.listToHide.size(); i++) {
					MyPhotoAdapter.listToHide.get(i).hide();
					if(MyHiddenPhotoActivity.listAfterRestore!=null){
						MyHiddenPhotoActivity.listAfterRestore.add(MyPhotoAdapter.listToHide.get(i));
					}
					listAfterHide.remove(MyPhotoAdapter.listToHide.get(i));
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
				bindView(listAfterHide);
			}
		};
		task.execute();
	}


	protected void bindView(List<MyPhoto> list) {
		photoAdapter = new MyPhotoAdapter(this, R.layout.my_photo,
				list);
		listPhoto.setAdapter(photoAdapter);
	}
	
}
